package com.xbongbong.outerlink.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.outerlink.AbstractProOuterLinkService;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.dto.OuterLinkBaseDTO;
import com.xbongbong.paas.pojo.dto.ValidateDataDTO;
import com.xbongbong.paas.pojo.vo.AfterSaveVO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.pojo.vo.ParseSingleRowDataVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.purchaseinvoice.dto.PurchaseInvoiceAmountDetailGetDTO;
import com.xbongbong.pro.purchaseinvoice.vo.PurchaseInvoiceAmountDetailVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderDataEsListVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.InvoiceConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.PaySheetEnum;
import com.xbongbong.saas.enums.business.PurchaseInvoiceEnum;
import com.xbongbong.saas.help.InvoiceCommonHelp;
import com.xbongbong.saas.model.PayPlanSheetModel;
import com.xbongbong.saas.service.PurchaseInvoiceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 采购发票 外链接口实现
 * @author zheng.li
 **/
@Service
public class PurchaseInvoiceOuterLinkServiceImpl extends AbstractProOuterLinkService {

    @Resource
    private PurchaseInvoiceService purchaseInvoiceService;

    @Resource
    private PayPlanSheetModel payPlanSheetModel;


    @Override
    public List<Integer> businessType() {
        return Collections.singletonList(XbbRefTypeEnum.PURCHASE_INVOICE.getCode());
    }

    @Override
    public void handleDataList(FormDataListDTO formDataListDTO, PaasFormDataEsListVO paasFormDataEsListVO, WorkOrderDataEsListVO workOrderDataEsListVO) throws XbbException {
        super.handleDataList(formDataListDTO,paasFormDataEsListVO,workOrderDataEsListVO);
    }


    @Override
    public void handleGetData(OuterLinkBaseDTO outerLinkBaseDTO,
                              PaasFormDataEntityExt formData, ParseSingleRowDataVO parseSingleRowDataVO, List<FieldAttrEntity> explains) throws XbbException {

        String corpid = outerLinkBaseDTO.getCorpid();
        JSONObject newData = parseSingleRowDataVO.getResult();
        PurchaseInvoiceAmountDetailGetDTO purchaseInvoiceAmountDetailGetDTO = InvoiceCommonHelp.getAmountDetailByInvoiceData(formData);
        BeanUtil.copyProperties(outerLinkBaseDTO, purchaseInvoiceAmountDetailGetDTO);
        purchaseInvoiceAmountDetailGetDTO.setIsEdit(BasicConstant.ONE);
        JSONArray originObject = newData.getJSONArray(PurchaseInvoiceEnum.ORIGIN_ID.getAttr());
        Long originId = 0L;
        if (Objects.nonNull(originObject)) {
            originId = originObject.getJSONObject(BasicConstant.ZERO).getLong(BasicConstant.ID);
        }

        if (originId > 0L) {
            purchaseInvoiceAmountDetailGetDTO.setFormRed(BasicConstant.ONE);
        }
        PurchaseInvoiceAmountDetailVO purchaseInvoiceAmountDetailVO = purchaseInvoiceService.getPurchaseInvoiceAmountDetail(purchaseInvoiceAmountDetailGetDTO);
        if (Objects.equals(purchaseInvoiceAmountDetailGetDTO.getIsEdit(), BasicConstant.ONE) && Objects.equals(purchaseInvoiceAmountDetailGetDTO.getIsRed(), BasicConstant.ONE)) {
            if (Objects.nonNull(newData)) {
                double invoiceAmount = newData.getDouble(PurchaseInvoiceEnum.TOTAL_MONEY.getAttr());
                newData.put(PurchaseInvoiceEnum.TOTAL_MONEY.getAttr(), -invoiceAmount);
            }
        }
        JSONArray amountDetail = JSONArray.parseArray(JSONArray.toJSONString(purchaseInvoiceAmountDetailVO.getAmountDetail()));
        JSONArray paySheetArray = newData.getJSONArray(PurchaseInvoiceEnum.PAY_SHEET_ID.getAttr());
        List<Long> paySheetiList = new ArrayList<>();
        if (CollectionsUtil.isNotEmpty(paySheetArray)) {
            for (int i = 0; i < paySheetArray.size(); i++) {
                JSONObject paySheetObject = paySheetArray.getJSONObject(i);
                paySheetiList.add(paySheetObject.getLong(BasicConstant.ID));
            }
            Map<Long, String> paySheetTypeMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            map.put("corpid", corpid);
            map.put("idIn", paySheetiList);
            map.put("del", BasicConstant.ZERO);
            List<PaasFormDataEntityExt> paySheetEntityList = payPlanSheetModel.findEntitys(map);
            paySheetEntityList.forEach(item -> {
                JSONObject jsonObject = item.getData();
                String paySheetType = jsonObject.getString(PaySheetEnum.PAY_SHEET_TYPE.getAttr());
                paySheetTypeMap.put(item.getId(), paySheetType);
            });
            for (int i = 0; i < paySheetArray.size(); i++) {
                JSONObject paySheetObject = paySheetArray.getJSONObject(i);
                String paySheetType = paySheetTypeMap.getOrDefault(paySheetObject.getLong(BasicConstant.ID), "");
                paySheetObject.put(InvoiceConstant.PAY_SHEET_TYPE, paySheetType);
            }
        }
        newData.put(PaasConstant.AMOUNTDETAIL, amountDetail);
    }


    @Override
    public void beforeSave(ValidateDataDTO validateDataDTO) throws XbbException {

    }

    @Override
    public AfterSaveVO insert(FormDataAddDTO formDataAddDTO, PaasFormDataEntity paasFormDataEntity, PaasFormEntityExt paasFormEntityExt) throws XbbException {
        return null;
    }
}
